package cn.crm.Service.Impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.crm.Service.ICusDevPlanService;
import cn.crm.common.ServerResponse;
import cn.crm.entity.CusDevPlan;
import cn.crm.entity.Order;
import cn.crm.mapper.CusDevPlanMapper;
import cn.crm.mapper.OrderMapper;
import cn.crm.mapper.SaleChanceMapper;
@Service
public class CusDevPlanServiceImpl implements ICusDevPlanService{

	@Autowired
	private CusDevPlanMapper cusdevplanmapper;
	@Autowired 
	private SaleChanceMapper salechancemapper;
	@Autowired
	private OrderMapper ordermapper;
	
	@Override
	public ServerResponse insert(CusDevPlan cusDevPlan,Integer devResult) {
		// TODO Auto-generated method stub
		int count = cusdevplanmapper.insert(cusDevPlan);
		int id = cusDevPlan.getSaleChanceId();
		salechancemapper.updateById(devResult,id);
		if (devResult == 2) {
			Order order = new Order();
			int productId = salechancemapper.selectproductId(id);
			order.setProductId(productId);
			int customerId = salechancemapper.selectcustomerId(id);
			order.setCustomerId(customerId);
			Date date = new Date();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
			String newdate = simpleDateFormat.format(date);
			order.setOrderNo(newdate);
			order.setSaleChanceId(id);
			order.setOrderDate(cusDevPlan.getPlanDate());
			order.setStatus(0);
			System.err.println("111111111111111111111111111111111111111111111111111111111111111111111111111111111");
			System.err.println(order);
			ordermapper.insert(order);
		}
		if (count == 1) {
			return ServerResponse.createSuccess("添加成功");
		}else {
			return ServerResponse.createError("添加失败");
		}
		
	}

}
